/*
 * Copyright (C) 2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

/*
 * Prototypes
 */
#if PROTO
static void
fp_exp_fp(f_t *u, f_t *v, eint32_t m);

static void
dp_exp_dp(d_t *u, d_t *v, eint32_t m);

static void
vo_exp_vo__d(vo_t *u, vo_t *v, eint32_t m);

static void
vo_exp_vo__r(vo_t *u, vo_t *v, eint32_t m);
#endif

/*
 * Implementation
 */
#if CODE
void
evector_exp(evector_t *v, etype_t t)
{
    eint32_t	 l;
    evector_t	*r;
    etype_t	 rt;
    v_enter();

    v_check(2);
    l = v->length;

    rt = match(t, t, 0, 1);
    if (rt != t)
	v = coerce(v, t, rt);
    r = create(v, rt);

    switch (rt) {
	case t_float32:
	    fp_exp_fp(r->v.obj, v->v.obj, l);
	    break;
	case t_float64:
	    dp_exp_dp(r->v.obj, v->v.obj, l);
	    break;
	default:
	    if (!thread_self->flt)
		vo_exp_vo__d(r->v.obj, v->v.obj, l);
	    else
		vo_exp_vo__r(r->v.obj, v->v.obj, l);
	    break;
    }
    v_leave();
}

static void
fp_exp_fp(f_t *u, f_t *v, eint32_t m)
{
    for (; m; m--, u++, v++)
	*u = expf(*v);
}

static void
dp_exp_dp(d_t *u, d_t *v, eint32_t m)
{
    for (; m; m--, u++, v++)
	*u = exp(*v);
}

static void
vo_exp_vo__d(vo_t *u, vo_t *v, eint32_t m)
{
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_d(1.0);
		break;
	    case t_int:
		set_d(ed_exp_i(get_i(v)));
		break;
	    case t_float:
		set_d(ed_exp_d(get_d(v)));
		break;
	    case t_mpz:
		set_d(ed_exp_z(get_z(v)));
		break;
	    case t_mpq:
		set_d(ed_exp_q(get_q(v)));
		break;
	    case t_mpr:
		er_exp_r(thr_rr, get_r(v));
		set_r(thr_rr);
		break;
	    case t_cdd:
		set_dd(edd_exp_dd(get_dd(v)));
		break;
	    case t_cqq:
		set_dd(edd_exp_qq(get_qq(v)));
		break;
	    case t_mpc:
		ecc_exp_cc(thr_c, get_cc(v));
		set_cc(thr_c);
		break;
	    default:
		eraise_exception(except_not_a_number);
	}
    }
}

static void
vo_exp_vo__r(vo_t *u, vo_t *v, eint32_t m)
{
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		mpfr_set_ui(thr_rr, 1, thr_rnd);
		set_r(thr_rr);
		break;
	    case t_int:
		er_exp_i(thr_rr, get_i(v));
		set_r(thr_rr);
		break;
	    case t_float:
		set_d(ed_exp_d(get_d(v)));
		break;
	    case t_mpz:
		er_exp_z(thr_rr, get_z(v));
		set_r(thr_rr);
		break;
	    case t_mpq:
		er_exp_q(thr_rr, get_q(v));
		set_r(thr_rr);
		break;
	    case t_mpr:
		er_exp_r(thr_rr, get_r(v));
		set_r(thr_rr);
		break;
	    case t_cdd:
		set_dd(edd_exp_dd(get_dd(v)));
		break;
	    case t_cqq:
		ecc_exp_qq(thr_c, get_qq(v));
		set_cc(thr_c);
		break;
	    case t_mpc:
		ecc_exp_cc(thr_c, get_cc(v));
		set_cc(thr_c);
		break;
	    default:
		eraise_exception(except_not_a_number);
	}
    }
}
#endif
